package com.rxjava.jdbc;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import com.github.davidmoten.rx.jdbc.Database;

public class RxJdbcTest {
	public static Database db;
	public static final String url = "jdbc:mysql://localhost:3306/hiwetop?useUnicode=true&characterEncoding=utf8&autoReconnect=true&amp;failOverReadOnly=false";
	public static final String username = "root";
	public static final String password = "login";
	
	@Before
	public void before() {
		db = Database.from(url, username, password);
	}

	@Test
	public void test() {
		List<String> names = db
		        .select("select account from user_info where account = ? order by account")
		        .parameter("linyu")
		        .getAs(String.class)
		        .toList().toBlocking().single();
		assertEquals("linyu",names.get(0));
		System.out.println(names);
	}
	
	@Test
	public void test2(){
		List<String> names = db
				.select("select account from user_info ")
				.getAs(String.class)
				.filter(account -> account.startsWith("linyu"))
				.skip(3)
				.toList()
				.toBlocking().first();
		System.out.println(names);
	}
	
	@Test
	public void test3(){
		String name = db
				.select("select account from user_info ")
				.getAs(String.class)
				.first(account -> account.equals("linyu"))
				.toBlocking()
				.single();
		System.out.println(name);
	}

}
